package com.hndct.util;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 253云通讯对接
 * @author zs
 */
public class MsgUtil253 {
	private static final Logger logger = LoggerFactory.getLogger(MsgUtil253.class);
	
	/*
	 * 账号
	 */
	private static final String account = "I1041074";
	
	/*
	 * 密码
	 */
	private static final String password = "Pj7J1SRBLp9ae4";
	
	/*
	 * 网址
	 */
	private static final String api = "http://intapi.253.com/";
	
	/*
	 * 发送短信
	 */
	public static String sendMsg(String mobile,String msg){
		JSONObject param = new JSONObject();
		param.put("account", account);
		param.put("password", password);
		param.put("mobile", mobile);
		param.put("msg", msg);
		
		Map<String,String> header = new HashMap<String,String>();
		header.put("Content-Type","application/json");
		
		String res = HttpClientUtil.doPostJson(api+"/send/json",param.toString(),header);
		logger.info("服务商253发送短信接口返回：{}",res);
		
		JSONObject resObject = JSONObject.fromObject(res);
		String resCode = resObject.getString("code");
		String resError = resObject.getString("error");
		String msgId = resObject.getString("msgid");
		
		if("0".equals(resCode)){
			logger.info("{}短信发送成功：{} - msgId:{}",mobile,msg,msgId);
		}else{
			logger.info("{}短信发送失败{}，原因:{}-{} - msgId:{}",mobile,msg,resCode,resError,msgId);
		}
		
		return res;
	}
	
	/*
	 * 查询余额
	 */
	public static BigDecimal getBalance(){
		JSONObject param = new JSONObject();
		param.put("account", account);
		param.put("password", password);
		
		Map<String,String> header = new HashMap<String,String>();
		header.put("Content-Type","application/json");
		
		String res = HttpClientUtil.doPostJson(api+"/balance/json",param.toString(),header);
		logger.info("服务商253查询余额接口返回：{}",res);
		
		JSONObject resJson = JSONObject.fromObject(res);
		String balance = resJson.getString("balance");
		
		return new BigDecimal(balance);
	}
	
	/*public static void main(String[] args) {
		String res = getBalance();
		System.out.println(res);
	}*/
	
}
